package com.xlb.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xlb.domain.dos.ArticleDO;
import com.xlb.domain.mapper.ArticleMapper;
import com.xlb.domain.mapper.CategoryMapper;
import com.xlb.domain.mapper.TagMapper;
import com.xlb.web.model.vo.statistics.FindStatisticsInfoRspVO;
import com.xlb.web.service.StatisticsService;
import com.xlb.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小聋包
 * @date 2024/5/23
 * @Version 1.0
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private TagMapper tagMapper;

    /**
     * 获取文章总数，分类总数，标签总数，总访问量统计信息
     * @return
     */
    @Override
    public Response findInfo() {
       //查询文章总数
        Long articleTotalCount= articleMapper.selectCount(Wrappers.emptyWrapper());
        //查询分类总数
        Long categoryTotalCount=categoryMapper.selectCount(Wrappers.emptyWrapper());
        //查询标签总数
        Long tagTotalCount=tagMapper.selectCount(Wrappers.emptyWrapper());
        //查询总访问量
        List<ArticleDO> articleDOS=articleMapper.selectAllReadNum();
        Long pvTotalCount=0L;
        if (!CollectionUtils.isEmpty(articleDOS)){
           //所有read_num相加
            pvTotalCount=articleDOS.stream().mapToLong(ArticleDO::getReadNum).sum();
        }
        //组装VO类
        FindStatisticsInfoRspVO vo=FindStatisticsInfoRspVO.builder()
                .articleTotalCount(articleTotalCount)
                .categoryTotalCount(categoryTotalCount)
                .tagTotalCount(tagTotalCount)
                .pvTotalCount(pvTotalCount)
                .build();

        return Response.success(vo);
    }
}
